# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
from ganjiscrapy.db import BaseModel, Book


class GanjiscrapyPipeline(object):
    def process_item(self, item, spider):
        try:
            print(type(Book.get(Book.bookid==item['bookId'])))
        except Book.DoesNotExist:
            book1 = Book(**{'bookid':item['bookId'],'name':item['title'],'desc':item['desc'],'url':item['url'],'img':item['image'],'level':item['level'],'levelsub':item['levelSub'], 'status':item['status']})
            # book1 = Book(**{'bookid':bookId,'name':title,'desc':desc,'url':url,'img':image,'level':level,'levelsub':levelSub, 'status':status})
            book1.save()
        return item

# class GanjiscrapyPipeline(object):
#     def process_item(self, item, spider):
#         with open('./res.txt','a') as f:
#             f.write(item.get('body')+'\r\n')
#         return item
